# -*- coding: utf-8 -*-
# File : database_services.py
# Author: taoyahui
# Date : 2022/6/13

# 实现数据库相关操作
import os
import sqlite3

def execSQL(sql):
    conn = sqlite3.connect('students.db')
    cursor = conn.cursor()
    try:
        cursor.execute(sql)
        #         捕获执行SQL后的结果集
        result = cursor.fetchall()
        conn.commit()
        return result
    except BaseException as e:
        #         对指定的异常类型进行捕获，并将具体信息打印出来
        conn.rollback()
        print('错误: %s' % e)
    finally:
        conn.close()

def data_change(field_number, value, student_id):
    """
    实现数据库student表的修改
    :return:
    """
    conditions = ['id', 'name', 'gender', 'student_id', 'class_name', 'college_name']
    execSQL("update student set %s = '%s' where id = %s" % (conditions[field_number], value, student_id))
    print("修改成功！")
    results = execSQL("select * from student where id = %s" % student_id)
    print(results)
    return 0


def data_show():
    results = execSQL('select * from student')
    # show_student(results)
    student_list = []
    for result in results:
        student_list.append("{0}\t{1}\t{2}\t{3}\t{4}\t\t{5}".format(result[0], result[1], result[2], result[3], result[4], result[5]))
    return student_list

